package com.stream.topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;

/**
 * <p>
 *   stream3.0以上版本不建议使用input，output StreamListener注解所以换了新的函数式编程
 *   消费端与生产端用名称进行约束  例如生产端 testTopic-in-0  消费端用 testTopic就可以绑定
 * </p>
 *
 * @author gzj
 * @since 2022/9/23
 */
@Component
public class Topic {

    @Bean
    Consumer<String> testTopic(){
        return str -> {
            System.out.println("我是消费者0，我收到了消息："+str);
        };
    }

    @Bean
    Consumer<String> testTopic1(){
        return str -> {
            System.out.println("我是消费者1，我收到了消息："+str);
        };
    }

    @Autowired
    StreamBridge streamBridge;

    public void send(String message) {
//        streamBridge.send("testTopic-in-0", message);
        streamBridge.send("testTopic-in-0",  MessageBuilder
                .withPayload(message).setHeader("fooKey", "fooValue").setHeader("barKey", "barValue").build());
        System.out.println("************发送了message："+message);
    }


    public void send1(String message) {
        streamBridge.send("testTopic1-in-0", message);
        System.out.println("************发送了message："+message);
    }
}
